<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>过滤器</title>
    <script type="text/javascript" src="../js/vue.js"></script>
    <script src="../js/dayjs.min.js"></script>
</head>
<body>
    <!-- 准备一个容器 -->
    <div id="root">
        <!-- 计算属性实现 -->
        当前时间：{{compTime}} <br>
        <!-- 过滤器实现 -->
        当前时间：{{time | timeFmt('YYYY-MM-DD') | mySlice | addYear}}
    </div>

    <script type="text/javascript">
        // 全局过滤器
        Vue.filter('addYear',v => {return v + ' 年'})

        new Vue({
            el:'#root',
            data(){
                return{
                    time:'1981561377603'
                }
            },
            computed:{
                compTime(){
                    return dayjs(this.time).format('YYYY-MM-DD HH:mm:ss')
                }
            },
            //局部过滤器
            filters:{
                timeFmt(value,formatStr){
                    return dayjs(value).format(formatStr)
                },
                mySlice(value){
                    return value.slice(0,4)
                },
            }
        })
    </script>
</body>

</html>